package com.zmzncs.shop.module.shop.service.impl;

import com.zmzncs.shop.module.shop.mapper.ShopTicketFileMapper;
import org.springframework.beans.BeanUtils;
import com.zmzncs.shop.module.shop.pojo.shopticket.*;
import com.zmzncs.shop.module.shop.mapper.ShopTicketMapper;
import com.zmzncs.shop.module.shop.service.ShopTicketService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 高博文
 * @since 2021-05-25
 */
@Service
public class ShopTicketServiceImpl extends ServiceImpl<ShopTicketMapper, ShopTicket> implements ShopTicketService {

    @Autowired
    private ShopTicketMapper shopTicketMapper;
    @Autowired
    ShopTicketFileMapper shopTicketFileMapper;

    /**
    * 分页
    */
    @Override
    public IPage<ShopTicketDTO> page(ShopTicketSearchForm form){
        IPage<ShopTicketDTO> iPage = shopTicketMapper.list(form.getPage(), form);
        if (iPage != null &&iPage.getRecords() != null && iPage.getRecords().size() > 0){
            for (ShopTicketDTO record : iPage.getRecords()) {
                List<String> fileList = shopTicketFileMapper.getfileByTicketId(record.getId());
                if (fileList != null && fileList.size() > 0){
                    record.setImageUrl(fileList.get(0));
                }
            }
        }

        return iPage;
    }

    /**
    * 添加
    */
    @Override
    @Transactional
    public void add(ShopTicketAddForm form){
        ShopTicket shopTicket = new ShopTicket();
        BeanUtils.copyProperties(form, shopTicket);

        shopTicketMapper.insert(shopTicket);
    }

    /**
    * 删除
    */
    @Override
    @Transactional
    public void delete(Long id){
        ShopTicket shopTicket = new ShopTicket();
        shopTicket.setId(id);
        shopTicket.setIsDeleted(1);

        shopTicketMapper.updateById(shopTicket);
    }

    /**
    * 修改
    */
    @Override
    @Transactional
    public void update(ShopTicketUpdateForm form){
        ShopTicket shopTicket = new ShopTicket();
        BeanUtils.copyProperties(form, shopTicket);

        shopTicketMapper.updateById(shopTicket);
    }

    /**
    * 详情
    */
    @Override
    public ShopTicketDTO getById(Long id){
        ShopTicketDTO shopTicketDTO = shopTicketMapper.getById(id);

        return shopTicketDTO;
    }

}
